﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DigitalSolutionManagement
{
    public partial class frmPreStatistics : BaseFormEdit
    {
        private frmViewReport.ReportType rptType;

        public frmPreStatistics(frmViewReport.ReportType type)
        {
            InitializeComponent();
            rptType = type;
        }

        private void frmPreStatistics_Load(object sender, EventArgs e)
        {
            DataTable tbCombo = new DataTable();
            tbCombo.Columns.Add("type", typeof(int));
            tbCombo.Columns.Add("name", typeof(string));

            tbCombo.Rows.Add(0, "Hôm nay");
            tbCombo.Rows.Add(1, "Tuần này");
            tbCombo.Rows.Add(2, "Tháng này");
            tbCombo.Rows.Add(3, "Năm nay");
            tbCombo.Rows.Add(4, "Tùy chọn");

            cmbTime.DataSource = tbCombo;
            cmbTime.DisplayMember = "name";
            cmbTime.ValueMember = "type";
        }

        private void cmbTime_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbTime.SelectedIndex == 4)
            {
                txtEnd.Enabled = txtStart.Enabled = true;
            }
            else
            {
                txtEnd.Enabled = txtStart.Enabled = false;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (cmbTime.SelectedIndex == 4 && (txtStart.Empty || txtEnd.Empty || !txtStart.IsValidDate() || !txtEnd.IsValidDate()))
            {
                MessageBox.Show("Khoảng thời gian để thống kê không hợp lệ, vui lòng kiểm tra lại", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (cmbTime.SelectedIndex == 4 && (txtStart.Value > txtEnd.Value))
            {
                MessageBox.Show("Ngày bắt đầu không thể lớn hơn ngày kết thúc", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DateTime startDate = DateTime.MinValue, endDate = DateTime.MinValue;
            switch (cmbTime.SelectedIndex)
            {
                case 0:
                    startDate = endDate = DateTime.Today;
                    break;
                case 1:
                    int todayInWeek = Convert.ToInt16(DateTime.Today.DayOfWeek);
                    startDate = DateTime.Today.AddDays(-(6 - (6 - todayInWeek)));
                    endDate = DateTime.Today.AddDays(6 - todayInWeek);
                    break;
                case 2:
                    startDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
                    endDate = startDate.AddMonths(1).AddDays(-1);
                    break;
                case 3:
                    startDate = new DateTime(DateTime.Today.Year, 1, 1);
                    endDate = new DateTime(DateTime.Today.Year, 12, 31);
                    break;
                case 4:
                    startDate = txtStart.Value;
                    endDate = txtEnd.Value;
                    break;
            }

            frmViewReport frm = new frmViewReport(rptType);
            frm.StartDate = startDate;
            frm.EndDate = endDate;
            frm.Show();
        }
    }
}
